package cn.wowjoy.office.board;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import cn.wowjoy.office.BuildConfig;
import cn.wowjoy.office.baselivedata.appbase.NewMainApplication;
import cn.wowjoy.office.pm.view.PMBaseActivity;
import cn.wowjoy.office.utils.NetWorkUtils;

import static android.content.Context.WIFI_SERVICE;

public class NetBroadcastReceiver extends BroadcastReceiver {

    public NetChangeListener listener = PMBaseActivity.listener;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        // 如果相等的话就说明网络状态发生了变化
        if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            int netWorkState = NetWorkUtils.getNetWorkState(context);

            WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            Log.e("wifiInfo", wifiInfo.toString());
            Log.e("SSID",wifiInfo.getSSID());
            NewMainApplication.isWifi = BuildConfig.WIFI_NAME.equals(wifiInfo.getSSID());


            // 当网络发生变化，判断当前网络状态，并通过NetEvent回调当前网络状态
            if (listener != null) {
                listener.onChangeListener(netWorkState);
            }
        }
    }

    // 自定义接口
    public interface NetChangeListener {
        void onChangeListener(int status);
    }
}
